#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

export PATH=$TOP_DIR/cross/bin:$PATH

VER_OPT="20131021-2245"

# export MAKE_OPT="-j 2"

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -f x264-snapshot-$VER_OPT.tar.bz2 ] ; then
		rm -f x264-snapshot-*.tar.bz2
		wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-$VER_OPT.tar.bz2
	fi
	cd ..
fi


if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32"
	CROSS_PREFIX="x86_64-w64-mingw32-"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32"
		CROSS_PREFIX="i686-w64-mingw32-"
	fi
	rm -rf $BIN_DIR/x264
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_x264
	tar xjf $SRC_DIR/x264-snapshot-$VER_OPT.tar.bz2
	mv x264-snapshot-$VER_OPT bd_x264
	cd bd_x264
	# patch -p0 < $TOP_DIR/patch/x264-configure-w64.patch
	./configure $HOST_FLAG --cross-prefix=$CROSS_PREFIX --prefix=$BIN_DIR/x264 --enable-static 
	# --enable-shared

	make $MAKE_OPT
	make install
# rm $BIN_DIR/x264/lib/libx264.a
	cd ..
	cd ..
fi

